package com.hunk.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.util.Assert;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author Dengyx
 */
public class JacksonUtil {
	private static ObjectMapper oMapper = new ObjectMapper();
	
	/**@deprecated
	 * @param t
	 * @return
	 */
	public static <T> String getJsonStrFromBeanToList(T t) {
		String json = "";
		List<T> list = new ArrayList<T>();
		list.add(t);
		try {
			json = oMapper.writeValueAsString(list);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return json;
	}
	public static <T> String getJsonStrFromBean(T t) {
		Assert.notNull(t, "input param should not be null");
		String json = "";	
		try {
			json = oMapper.writeValueAsString(t);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return json;
	}
}
